#pragma once
#include <ctemplate/template.h>
#include <string>

// #include "oj_model.hpp"
#include "oj_model2.hpp"

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template_html/";

    class View
    {
    public:
        void ExpandAllQuestion(const vector<Question> &questions, string *html)
        {
            // 形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto &q : questions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }

            // 获取被渲染网页
            std::string src_html = template_path + "all_questions.html";
            ctemplate::Template *tql = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 添加字典数据到网页
            tql->Expand(html, &root);
        }
        void ExpandOneQuestion(const Question &q, string *html)
        {
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("star", q.star);
            root.SetValue("desc", q.desc);
            root.SetValue("pre_code", q.header);

            std::string src_html = template_path + "one_question.html";
            ctemplate::Template *tql = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            tql->Expand(html, &root);
        }
    };
}